package com.klod.base


import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.receiveAsFlow
import kotlinx.coroutines.launch
//,ViewModelHandler<D>
abstract class BaseViewModel<S:UiState,D>(viewState: D) :ViewModel() {



    var viewData by mutableStateOf(viewState)
        protected  set
    private val intentChannel = Channel<S>(Channel.BUFFERED)
    val uiState = intentChannel.receiveAsFlow()

    fun dropState(state: S) {
        viewModelScope.launch(Dispatchers.IO) {
            intentChannel.send(state)
        }
    }

//
//    private val intentChannel = Channel<I>(Channel.UNLIMITED)
//    protected val modelChannel = Channel<D>(Channel.UNLIMITED)
//    val modelState = modelChannel.receiveAsFlow()
//
//    var viewStates by mutableStateOf(viewState)
//        protected set
//
//    init {
//        handleIntent()
//    }
//
//    private fun handleIntent() {
//        viewModelScope.launch(Dispatchers.IO) {
//            intentChannel.consumeAsFlow().collect {
//                handleEvent(it, viewStates)
//            }
//        }
//    }
//

}